<?php
/**
 * 监控服务 ws http 8811
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/30 0030
 * Time: 10:50
 */
class Server
{
    const PORT = 8811;

    public function port()
    {
        //监听多少行
        $shell = "netstat -anp 2>/dev/null | grep " . self::PORT . ' | grep LISTEN | wc -l';
//        $shell = "netstat -anp 2>/dev/null | grep " . self::PORT . ' | grep LISTEN';
        $result = shell_exec($shell);
        if($result != 1){
            //todo
            echo date('Y-m-d H:i:s').'-error'.PHP_EOL;
        }else{
            echo date('Y-m-d H:i:s').'-success'.PHP_EOL;
        }
    }
}

//通过nobup .. & 在 后台持续监测，记录到server.txt中
// nohup php server.php > server.txt &
$nohup = "nohup /usr/local/php/bin/php /data/wwwroot/www/swoole-demo/mooc/tp5/script/monitor/server.php > /data/wwwroot/www/swoole-demo/mooc/tp5/script/monitor/server.txt &";

//swoole定时器，每2s监测一次
swoole_timer_tick(2000, function($timer_id){
    (new Server())->port();
    echo "timer-start".PHP_EOL;
});




